//
//  CLReadLeftView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/16.
//

#import "CLReadLeftView.h"

@interface CLReadLeftView ()<CLSegmentedControlDelegate>

@end

@implementation CLReadLeftView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews {
    // 工具栏
    CLSegmentedControl *segmentedControl = [[CLSegmentedControl alloc] initWithFrame: CGRectMake(0, CL_X_other(X_TOP, 0), CL_SIZE(CL_SPACE_335), CL_SIZE(CL_SPACE_50))];
    self.segmentedControl = segmentedControl;
    segmentedControl.delegate = self;
    segmentedControl.normalFont =  CL_FONT_SA_14;
    segmentedControl.normalColor =  CL_COLOR_145_145_145;
    segmentedControl.selectFont =  CL_FONT_SA_14;
    segmentedControl.sliderWidth =  CL_SPACE_SA_30;
    segmentedControl.sliderHeight =  CL_SPACE_SA_2;
    segmentedControl.selectColor =  CL_READ_COLOR_MAIN;
    segmentedControl.sliderColor =  CL_READ_COLOR_MAIN;
    [self addSubview:segmentedControl];
    
    [segmentedControl reloadTitles:@[@"目录",@"书签"]];
    
    // 分割线
    UIView *spaceLine = SpaceLine(self,  CL_COLOR_230_230_230);
    self.spaceLine = spaceLine;
    spaceLine.frame = CGRectMake(0, CGRectGetMaxY(segmentedControl.frame), CL_READ_LEFT_VIEW_WIDTH, CL_SPACE_LINE);
    
    // 目录
    CLReadCatalogView *catalogView = [[CLReadCatalogView alloc]init];
    self.catalogView = catalogView;
    [self addSubview:catalogView];
    catalogView.frame = CGRectMake(0, CGRectGetMaxY(spaceLine.frame), CL_READ_LEFT_VIEW_WIDTH, CL_READ_LEFT_VIEW_HEIGHT - CGRectGetMaxY(spaceLine.frame));
    
    // 书签
    CLReadMarkView *markView = [[CLReadMarkView alloc]init];
    self.markView = markView;
    markView.alpha = 0;
    [self addSubview:markView];
    markView.frame = catalogView.frame;
    
    // 更新当前UI
    [self updateUI];
}
/// 刷新UI 例如: 日夜间可以根据需求判断修改目录背景颜色,文字颜色等等
- (void)updateUI {
    
    // 日夜间切换修改
    if ([CLUserDefaults boolForKey:CL_READ_KEY_MODE_DAY_NIGHT]) {
        
        self.spaceLine.backgroundColor = [CL_COLOR_230_230_230 colorWithAlphaComponent:0.2];
        
        self.backgroundColor = CL_COLOR_46_46_46;
        
    }else{
        
        self.spaceLine.backgroundColor = CL_COLOR_230_230_230;
        
        self.backgroundColor = UIColor.whiteColor;
    }
    
    // 刷新分割线颜色(如果不需要刷新分割线颜色可以去掉,目前我是做了日夜间修改分割线颜色的操作)
    [self.catalogView.tableView reloadData];
    [self.markView.tableView reloadData];
}

- (void)segmentedControl:(CLSegmentedControl *)segmentedControl clickIndex:(NSInteger)index {
    [UIView animateWithDuration:CL_READ_AD_TIME animations:^{
        if (index == 0) { // 显示目录
            
            [self bringSubviewToFront:self.catalogView];
            self.catalogView.alpha = 1;
            self.markView.alpha = 0;
            
        }else{ // 显示书签
            
            [self bringSubviewToFront:self.markView];
            self.catalogView.alpha = 0;
            self.markView.alpha = 1;
        }
    }];
}
@end
